using TerritoryGame.Domain.Entities;
using TerritoryGame.Domain.ValueObjects;

namespace TerritoryGame.Infrastructure.Persistence;

public static class SeedData
{
    public static void Seed(GameDbContext context)
    {
        // 确保数据库已创建
        context.Database.EnsureCreated();

        // 检查是否已有数据
        if (context.GameRooms.Any())
        {
            return; // 如果已有数据，不重复添加
        }

        // 创建示例房间
        var rooms = CreateSampleRooms();
        context.GameRooms.AddRange(rooms);

        // 创建示例游戏
        var games = CreateSampleGames(rooms);
        context.Games.AddRange(games);

        // 保存更改
        context.SaveChanges();
    }

    private static List<GameRoom> CreateSampleRooms()
    {
        var rooms = new List<GameRoom>
        {
            new GameRoom(Guid.NewGuid(), "新手练习房", 6, null),
            new GameRoom(Guid.NewGuid(), "高手竞技场", 4, "password123"),
            new GameRoom(Guid.NewGuid(), "休闲涂色屋", 6, null),
            new GameRoom(Guid.NewGuid(), "速战速决", 3, null),
            new GameRoom(Guid.NewGuid(), "团队合作", 6, "team2024"),
            new GameRoom(Guid.NewGuid(), "创意工坊", 4, null),
            new GameRoom(Guid.NewGuid(), "竞技大师", 4, "master2024"),
            new GameRoom(Guid.NewGuid(), "欢乐时光", 6, null)
        };

        // 为每个房间添加一些示例玩家
        var playerNames = new[]
        {
            "涂色大师", "创意达人", "艺术爱好者", "游戏玩家",
            "色彩专家", "绘画新手", "设计天才", "美术老师",
            "学生小明", "程序员小王", "设计师小李", "艺术家小张"
        };

        var colors = PlayerColor.AvailableColors;
        var random = new Random();

        foreach (var room in rooms)
        {
            var playerCount = random.Next(1, Math.Min(4, room.MaxPlayers + 1));
            var selectedNames = playerNames.OrderBy(x => random.Next()).Take(playerCount).ToArray();

            for (int i = 0; i < playerCount; i++)
            {
                var player = new Player(
                    Guid.NewGuid(),
                    selectedNames[i],
                    colors[i % colors.Length]
                );
                room.AddPlayer(player);
            }
        }

        return rooms;
    }

    private static List<Game> CreateSampleGames(List<GameRoom> rooms)
    {
        var games = new List<Game>();
        var random = new Random();

        // 为一些房间创建进行中的游戏
        foreach (var room in rooms.Take(3))
        {
            var game = new Game(Guid.NewGuid(), room);

            // 设置游戏状态为进行中
            game.StartGame();

            // 模拟一些涂色操作
            for (int i = 0; i < 50; i++)
            {
                var x = random.Next(0, 800);
                var y = random.Next(0, 600);
                var brushSize = random.Next(1, 5);
                var position = new Position(x, y);
                
                // 随机选择一个玩家进行涂色
                if (room.Players.Any())
                {
                    var randomPlayer = room.Players[random.Next(room.Players.Count)];
                    try
                    {
                        game.ProcessPaintAction(randomPlayer.Id, position, brushSize);
                    }
                    catch
                    {
                        // 忽略涂色失败的情况
                    }
                }
            }

            games.Add(game);
        }

        return games;
    }
}
